package com.huilian.cloud.module.system.controller.admin.menugroup;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.huilian.cloud.framework.common.pojo.PageParam;
import com.huilian.cloud.framework.common.pojo.PageResult;
import com.huilian.cloud.framework.common.pojo.CommonResult;
import com.huilian.cloud.framework.common.util.object.BeanUtils;
import static com.huilian.cloud.framework.common.pojo.CommonResult.success;

import com.huilian.cloud.framework.excel.core.util.ExcelUtils;

import com.huilian.cloud.framework.apilog.core.annotation.ApiAccessLog;
import static com.huilian.cloud.framework.apilog.core.enums.OperateTypeEnum.*;

import com.huilian.cloud.module.system.controller.admin.menugroup.vo.*;
import com.huilian.cloud.module.system.dal.dataobject.menugroup.MenuGroupDO;
import com.huilian.cloud.module.system.service.menugroup.MenuGroupService;

@Tag(name = "管理后台 - 菜单分组类型关联")
@RestController
@RequestMapping("/sys/menu-group")
@Validated
public class MenuGroupController {

    @Resource
    private MenuGroupService menuGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建菜单分组类型关联")
    @PreAuthorize("@ss.hasPermission('sys:menu-group:create')")
    public CommonResult<Integer> createMenuGroup(@Valid @RequestBody MenuGroupSaveReqVO createReqVO) {
        return success(menuGroupService.createMenuGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新菜单分组类型关联")
    @PreAuthorize("@ss.hasPermission('sys:menu-group:update')")
    public CommonResult<Boolean> updateMenuGroup(@Valid @RequestBody MenuGroupSaveReqVO updateReqVO) {
        menuGroupService.updateMenuGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除菜单分组类型关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:menu-group:delete')")
    public CommonResult<Boolean> deleteMenuGroup(@RequestParam("id") Integer id) {
        menuGroupService.deleteMenuGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得菜单分组类型关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:menu-group:query')")
    public CommonResult<MenuGroupRespVO> getMenuGroup(@RequestParam("id") Integer id) {
        MenuGroupDO menuGroup = menuGroupService.getMenuGroup(id);
        return success(BeanUtils.toBean(menuGroup, MenuGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得菜单分组类型关联分页")
    @PreAuthorize("@ss.hasPermission('sys:menu-group:query')")
    public CommonResult<PageResult<MenuGroupRespVO>> getMenuGroupPage(@Valid MenuGroupPageReqVO pageReqVO) {
        PageResult<MenuGroupDO> pageResult = menuGroupService.getMenuGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MenuGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出菜单分组类型关联 Excel")
    @PreAuthorize("@ss.hasPermission('sys:menu-group:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMenuGroupExcel(@Valid MenuGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MenuGroupDO> list = menuGroupService.getMenuGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "菜单分组类型关联.xls", "数据", MenuGroupRespVO.class,
                        BeanUtils.toBean(list, MenuGroupRespVO.class));
    }

}